/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-08-25
 * 17:23
 */
public class Test {
    public static void main(String[] args) {

    }
}
class Solution {
    public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
    public int postindex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postindex=postorder.length-1;
        return buildTreeChild(inorder,postorder,0,postorder.length-1);
    }
    public TreeNode buildTreeChild(int[] inorder, int[] postorder,int begin,int inend){
        if(begin>inend){
            return null;
        }
        TreeNode root=new TreeNode(postorder[postindex]);
        int rootindex=find(inorder,inend,postorder[postindex]);
        postindex--;
        root.right=buildTreeChild(inorder,postorder,rootindex+1,inend);
        root.left=buildTreeChild(inorder,postorder,begin,rootindex-1);

        return root;
    }
    public int find( int[] inorder, int inend,int val){
        for(int j=0;j<=inend;j++){
            if(val==inorder[j]){
                return j;
            }
        }
        return -1;
    }
}